home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / C++ AppleLink Messages / CPlus.Dev$ 4⁄27⁄90 / 0112-Value parameter poly-Apr90 < prev    next >
Encoding:
Text File  |  1990-04-27  |  1.6 KB  |  42 lines  |  [TEXT/GEOL]

  1. Item    9254241                         26-April-90        13:17PDT
  2.  
  3. From:   DEREK                           White, Derek
  4.  
  5. To:     CPLUS.DEV$                      C++ Interest List--Developers
  6.         CPLUS.APPLE$                    C++ Interest List--Apple Employees
  7.  
  8. Sub:    Value parameter polymorphism
  9.  
  10.     Somewhere along the line I got the impression that when you pass object by
  11. value, a temporary of the ACTUAL parameter type is created, the copy
  12. constructor of the ACTUAL type is called on the temp, and then the address of
  13. the temp is passed to the function.  Then inside the function, all virtual
  14. functions would be called through the vtable.  This would allow polymorphism
  15. for value parameters.  But I tried an example, and CFront always created a a
  16. temp of the FORMAL parameter class, and called the FORMAL copy constructor.
  17. Furthermore, inside the function, virtual methods were called virtually, even
  18. though the function only gets parameters of the FORMAL type.  Am I correct?  If
  19. so, why does the function get called virtually?
  20.  
  21. class TShape { /* copy contructor & virtual Draw func */ };
  22. class TRect : public TShape {/* copy contructor & virtual Draw func */};
  23.  
  24. void valtest(TShape s)
  25. {   s.Draw();  } // virtual call, even though "s" is ALWAYS a TShape
  26.  
  27. main() {
  28.     TRect realRect;
  29.     TRect &aRect = realRect;
  30.     TShape realShape;
  31.     TShape &aShape = realShape;
  32.  
  33.     valtest(realRect);  // make temp TShape with TShape(realRect)
  34.     valtest(aRect);  // make temp TShape with TShape(aRect)
  35.     valtest(realShape); aRect
  36.     valtest(aShape);
  37. }
  38.  
  39. - Derek White
  40. ATG East/Columbia
  41.  
  42.